package com.ailiwu.www.ailiwu.Fragment;


import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.base.BaseFragment;
import com.ailiwu.www.ailiwu.bean.HomeTabs;
import com.ailiwu.www.ailiwu.url.Urls;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:    XiYoung
 * Version    V1.0
 * Date:      2015/12/2 0002 16:31
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/12/2 0002       XiYoung              1.0                   1.0
 * 2015/12/3 0003       XiYoung              2.0                    添加Tab栏目
 * 2015/12/3 0003       XiYoung              3.0                    添加网络加载
 * Why & What is modified:
 */
public class HomeFragment extends BaseFragment {

    protected ArrayList<Fragment> fragments = new ArrayList<>();
    protected ArrayList<String> titleList = new ArrayList<>();

    private Adapter adapter;
    private ViewPager viewPager;
    private TabLayout tabLayout;

    private ImageView ivChannerl;

    @Override
    protected View inflateView() {
        return View.inflate(getActivity(), R.layout.fragment_home, null);
    }

    @Override
    protected void initView(View view) {
//        ivChannerl = (ImageView) view.findViewById(R.id.channerl);
       /* ivChannerl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mAcAvtivity, UserChannelActivity.class));
            }
        });*/
        /**设置ViewPager和Tab的适配*/
        setTabViewPager(view);
    }

    @Override
    protected void initData() {
        // TODO: 2015/12/3 0003 判断数据库

        httpUtils.send(HttpRequest.HttpMethod.GET, Urls.URL_HOME_TAB, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                parsTabJson(result);
                // TODO: 2015/12/3 0003 存入更新数据库
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onFailure(HttpException e, String s) {
                // TODO: 2015/12/3 0003 网络连接失败
                Snackbar.make(tabLayout, "哎呀！网络连接失败了！！", Snackbar.LENGTH_LONG).show();
            }
        });
    }

    private void setTabViewPager(View view) {
        viewPager = (ViewPager) view.findViewById(R.id.pager);
        tabLayout = (TabLayout) view.findViewById(R.id.tabs_main);
        //getChildFragment
        adapter = new Adapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);

    }

    //避免重复加载
    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if (getView() != null) {
            getView().setVisibility(menuVisible ? View.VISIBLE : View.INVISIBLE);
        }
    }



    /**解析Tab栏目的Json字符串*/
    protected void parsTabJson(String result) {
        titleList.clear();
        fragments.clear();
        if (result != null) {
            HomeTabs homeTabs = gson.fromJson(result, HomeTabs.class);
            List<HomeTabs.DataEntity.ChannelsEntity> channels = homeTabs.getData().getChannels();
            for (int i = 0; i < channels.size(); i++) {
                titleList.add(channels.get(i).getName());
                //TODO 更改Fragents的添加
                if (i==0) {
                    fragments.add(new HomeFristFragment());
                }else {
                    fragments.add(new BaseHomeFragment().newInstance(channels.get(i).getName(),channels.get(i).getId()));
                }
            }
            tabLayout.setupWithViewPager(viewPager);

        }
    }


    /**
     * Tab与Viewpager适配器
     */
    class Adapter extends FragmentPagerAdapter {

        public Adapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {


            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titleList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
        }
    }
}
